// ----------------------------------------------------------------------------
// Copyright 2006-2008, GeoTelematic Solutions, Inc.
// All rights reserved
// ----------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Change History:
//  2008/02/21  Martin D. Flynn
//     -Moved from MenuBar.java
//  2008/02/27  Martin D. Flynn
//     -Added ability to specify the 'MenuBar.js' location in the 'webapp.conf' file.
// ----------------------------------------------------------------------------
// External dependencies:
//  - 'mnubarCreateSubMenu' must be defined externally.
//    'mnubarShowMenu' will call 'mnubarCreateSubMenu' to create the sub-menu with appropriate links.
// ----------------------------------------------------------------------------

var MenuTimeoutMS = 3500;

var mbSubMenuObj       = null;
var mbSelectedMenu     = null;
var mbDestroyMenuTimer = null;

/* stop the 'destroy-menubar' timer */
function mnubarClearDestroyMenuTimer() 
{
  if (mbDestroyMenuTimer != null) {
    clearTimeout(mbDestroyMenuTimer);
    mbDestroyMenuTimer = null;
  }
}

/* start the 'destroy-menubar' timer */
function mnubarSetDestroyMenuTimer() 
{
  mnubarClearDestroyMenuTimer();
  mbDestroyMenuTimer = setTimeout('mnubarDestroyMenu()',MenuTimeoutMS);
}

/* destroy current submenu */
function mnubarDestroyMenu() 
{
  if (mbSubMenuObj != null) {
    document.body.removeChild(mbSubMenuObj);
    mbSubMenuObj = null;
  }
  if (mbSelectedMenu != null) {
    mbSelectedMenu.className = 'menuBarUnsW';
    mbSelectedMenu = null;
  }
  mnubarClearDestroyMenuTimer();
}

/* show submenu */
function mnubarShowMenu(menuId) 
{
  mnubarDestroyMenu();
  var menuObj = document.getElementById(menuId);
  if (menuObj) {
    mbSelectedMenu = menuObj;
    mbSelectedMenu.className = 'menuBarSelW';
    var myMenu = mnubarCreateSubMenu(mbSelectedMenu);
    myMenu.style.visibility = 'visible';
  }
  mnubarSetDestroyMenuTimer();
}

/* toggle submenu (show if hidden, destroy if visible) */
function mnubarToggleMenu(menuId) 
{
  var menuObj = document.getElementById(menuId);
  if (!(menuObj)) {
    mnubarDestroyMenu();
    return;
  }
  if (menuObj == mbSelectedMenu) {
    mnubarDestroyMenu();
    return;
  }
  mnubarShowMenu(menuId);
}

/* create submenu frame */
function mnubarCreateMenuFrame(absLoc, absSiz, height) 
{
  var menuObj = document.createElement('div');
  menuObj.id             = 'subMenu';
  menuObj.name           = 'subMenu';
  menuObj.style.width    = absSiz.width + 'px';
  menuObj.style.left     = absLoc.left + 'px';
  menuObj.style.top      = (absLoc.top + absSiz.height) + 'px';
  menuObj.style.height   = height + 'px';
  menuObj.style.position = 'absolute';
  menuObj.style.cursor   = 'default';
  menuObj.style.zIndex   = 30000;
  return menuObj;
}

// ---
